package com.ruoyi.web.controller.sxt;

import java.util.List;
import javax.servlet.http.HttpServletResponse;


import com.ruoyi.common.core.domain.entity.SxtBumen;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.sxt.service.ISxtBumenService;
import com.ruoyi.system.service.ISysDeptService;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;

import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 部门Controller
 * 
 * @author ruoyi
 * @date 2025-04-15
 */
@RestController
@RequestMapping("/sxt/bumen")
public class SxtBumenController extends BaseController
{
    @Autowired
    private ISxtBumenService sxtBumenService;

    @Autowired
    private ISxtBumenService bumenService;

    /**
     * 查询部门列表
     */
    @PreAuthorize("@ss.hasPermi('sxt:bumen:list')")
    @GetMapping("/list")
    public TableDataInfo list(SxtBumen sxtBumen)
    {
        //startPage();
        List<SxtBumen> list = sxtBumenService.selectSxtBumenList(sxtBumen);
        return getDataTable(list);
    }

    /**
     * 导出部门列表
     */
    @PreAuthorize("@ss.hasPermi('sxt:bumen:export')")
    @Log(title = "部门", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SxtBumen sxtBumen)
    {
        List<SxtBumen> list = sxtBumenService.selectSxtBumenList(sxtBumen);
        ExcelUtil<SxtBumen> util = new ExcelUtil<SxtBumen>(SxtBumen.class);
        util.exportExcel(response, list, "部门数据");
    }

    /**
     * 获取部门详细信息
     */
    @PreAuthorize("@ss.hasPermi('sxt:bumen:query')")
    @GetMapping(value = "/{deptId}")
    public AjaxResult getInfo(@PathVariable("deptId") Long deptId)
    {
        return success(sxtBumenService.selectSxtBumenByDeptId(deptId));
    }

    /**
     * 查询部门列表（排除节点）
     */
    @PreAuthorize("@ss.hasPermi('system:dept:list')")
    @GetMapping("/exclude/{deptId}")
    public AjaxResult excludeChild(@PathVariable("deptId") Long deptId)
    {
        List<SxtBumen> bumenList = sxtBumenService.selectSxtBumenList(new SxtBumen());
        bumenList.removeIf(d -> d.getDeptId().intValue() == deptId || ArrayUtils.contains(StringUtils.split(d.getAncestors(), ","), deptId + ""));
        return success(bumenList);
    }

    /**
     * 新增部门
     */
    @PreAuthorize("@ss.hasPermi('sxt:bumen:add')")
    @Log(title = "部门", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SxtBumen sxtBumen)
    {
        return toAjax(sxtBumenService.insertSxtBumen(sxtBumen));
    }

    /**
     * 修改部门
     */
    @PreAuthorize("@ss.hasPermi('sxt:bumen:edit')")
    @Log(title = "部门", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SxtBumen sxtBumen)
    {
        return toAjax(sxtBumenService.updateSxtBumen(sxtBumen));
    }

    /**
     * 删除部门
     */
    @PreAuthorize("@ss.hasPermi('sxt:bumen:remove')")
    @Log(title = "部门", businessType = BusinessType.DELETE)
	@DeleteMapping("/{deptIds}")
    public AjaxResult remove(@PathVariable Long[] deptIds)
    {
        return toAjax(sxtBumenService.deleteSxtBumenByDeptIds(deptIds));
    }
//    /**
//     * 获取部门树列表
//     */
//    @PreAuthorize("@ss.hasPermi('system:user:list')")
//    @GetMapping("/bumenTree")
//    public AjaxResult deptTree(SxtBumen bumen)
//    {
//        return success(bumenService.selectBumenTreeList(bumen));
//    }
}
